package com.luomo.wenyuan.modules.wenyuan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.dao.TOrderBookDao;
import com.luomo.wenyuan.modules.wenyuan.entity.*;
import com.luomo.wenyuan.modules.wenyuan.service.*;
import com.luomo.wenyuan.modules.wenyuan.vo.TOrderBookVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TOrderShowVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author
 * @email
 * @date 2020-10-19 15:58:39
 */
@RestController
@RequestMapping("generator/torder")
public class TOrderController {
    @Autowired
    private TOrderService tOrderService;

    @Autowired
    private TOrderBookService tOrderBookService;

    @Autowired
    private TSupplierService tSupplierService;

    @Autowired
    private TOrderBookDao tOrderBookDao;

    @Autowired
    private TBookService tBookService;

    @Autowired
    private TPurchaseBookService tPurchaseBookService;

    @Autowired
    private TPurchaseApplicationService tPurchaseApplicationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tOrderService.queryPage(params);
        List<TOrderEntity> list = (List<TOrderEntity>) page.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            List<TOrderShowVo> tOrderShowVos = new ArrayList<>();
            TOrderShowVo showVo;
            for (TOrderEntity tOrderEntity : list) {
                showVo = new TOrderShowVo();
                BeanUtil.copyProperties(tOrderEntity, showVo);
                Integer supplierId = tOrderEntity.getSupplierId();
                showVo.setSupplierName(tSupplierService.getById(supplierId).getName());
                showVo.setBookNameList(tOrderService.getBookNameStrByOrderId(tOrderEntity.getId()));
                tOrderShowVos.add(showVo);
            }
            page.setList(tOrderShowVos);
        }
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TOrderEntity tOrder = tOrderService.getById(id);
        TOrderShowVo showVo = new TOrderShowVo();
        BeanUtil.copyProperties(tOrder, showVo);
        Integer supplierId = tOrder.getSupplierId();
        showVo.setSupplierName(tSupplierService.getById(supplierId).getName());
        List<String> booksStr = new ArrayList<>();
        List<TOrderBookEntity> books = tOrderBookDao.getByOrderId(tOrder.getId());
        List<TOrderBookVo> tOrderBookVos = new ArrayList<>();
        TOrderBookVo tOrderBookVo;
        for (TOrderBookEntity book : books) {
            TBookEntity tBookEntity = tBookService.getById(book.getBookId());
            tOrderBookVo = new TOrderBookVo();
            BeanUtil.copyProperties(book,tOrderBookVo);
            tOrderBookVo.setBook(tBookEntity);
            tOrderBookVo.setPurchaseId(book.getPurchaseId());
            booksStr.add(tBookEntity.getName());
            tOrderBookVos.add(tOrderBookVo);
        }
        showVo.setBookList(tOrderBookVos);
        showVo.setBookNameList(StrUtil.join(",", booksStr));
        return R.ok().put("tOrder", showVo);
    }

    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/save")
    public R save(@RequestBody TOrderVo orderVo) {
        TOrderEntity tOrder = new TOrderEntity();
        BeanUtil.copyProperties(orderVo, tOrder);
        tOrder.setOrderDate(new Date());
        tOrder.setOrderStatus(1);
        tOrderService.save(tOrder);
        Integer orderId = tOrder.getId();
        List<TOrderBookEntity> bookList = orderVo.getBookList();
        List<TOrderBookEntity> orderBookEntities = new ArrayList<>();
        Set<Integer> purchaseIdList = new HashSet<>();
        TOrderBookEntity tOrderBookEntity;
        for (TOrderBookEntity book : bookList) {
            tOrderBookEntity = new TOrderBookEntity();
            Integer purchaseId = book.getPurchaseId();
            tOrderBookEntity.setPurchaseId(purchaseId);
            tOrderBookEntity.setBookId(book.getBookId());
            tOrderBookEntity.setOrderId(orderId);
            tOrderBookEntity.setNum(book.getNum());
            orderBookEntities.add(tOrderBookEntity);
            purchaseIdList.add(purchaseId);
        }
        tOrderBookService.saveBatch(orderBookEntities);
        //更新采购申请状态
        for (Integer purchaseId : purchaseIdList) {
            boolean finish = true;
            List<TPurchaseBookEntity> purchaseBookEntities = tPurchaseBookService.getByPurchaseId(purchaseId);
            for (TPurchaseBookEntity purchaseBookEntity : purchaseBookEntities) {
                Integer readyNum = tOrderBookService.getBookNumByPurchaseIdAndBookId(purchaseId, purchaseBookEntity.getBookId());
                readyNum = readyNum == null ? 0 : readyNum;
                Integer num = purchaseBookEntity.getNum();
                if (num - readyNum != 0) {
                    //未采购完成
                    finish = false;
                }
            }
            if (finish) {
                UpdateWrapper<TPurchaseApplicationEntity> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", purchaseId);
                updateWrapper.set("status", 3);
                tPurchaseApplicationService.update(updateWrapper);
            }
        }
        return R.ok();
    }

    /**
     * 发货
     */
    @RequestMapping("/send")
    public R send(@RequestBody TOrderEntity tOrder) {
        UpdateWrapper<TOrderEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", tOrder.getId());
        updateWrapper.set("order_status", 2);
        updateWrapper.set("send_info", tOrder.getSendInfo());
        updateWrapper.set("send_date", new Date());
        updateWrapper.set("note", tOrder.getNote());
        tOrderService.update(updateWrapper);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TOrderEntity tOrder) {
        tOrderService.updateById(tOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tOrderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
